package com.ruoyi.common.core.utils.list;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import org.apache.poi.ss.formula.functions.T;

import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 集合工具类
 *
 * @author songpin566
 * @date 2023/09/15
 **/
public class ListUtils {

    public static <K, T, V> Map<K, V> toMap(Iterable<T> iter,
                                            Map<K, V> map,
                                            Function<T, K> keyMapper,
                                            Function<T, V> valMapper
    ) {
        if (iter == null) {
            return map;
        }

        if (map == null) {
            map = new HashMap<>();
        }

        Iterator<T> iterator = iter.iterator();
        try {
            while (iterator.hasNext()) {
                T next = iterator.next();
                K k = keyMapper.apply(next);
                V v = valMapper.apply(next);
                map.put(k, v);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return map;
    }


}
